/*
文章管理 对应 api 接口
其中 web 端没有接口
EditArt 编辑文章 函数未使用
*/

package v1

import (
    "math"
    "net/http"
    "strconv"

    "gitee.com/keeving/quanta-test-cat/middleware"
    "gitee.com/keeving/quanta-test-cat/model"
    "gitee.com/keeving/quanta-test-cat/utils/errmsg"

    "github.com/gin-gonic/gin"
)

// AddArticle 添加文章
func AddArticle(c *gin.Context) {
    var data model.Article
    token := c.GetHeader("Authorization")
    username, _ := middleware.GetUserNameFromToken(token)
    userID, _ := model.GetUserIDByUsername(username)

    title := c.PostForm("title")
    cid := c.PostForm("module")
    content := c.PostForm("content")
    desc := c.PostForm("desc")
    img := UploadFiles(c)

    data.UserID = userID
    data.Title = title
    data.Cid, _ = strconv.Atoi(cid)
    data.Content = content
    data.Desc = desc
    data.Img = img

    code := model.CreateArt(&data)

    c.JSON(http.StatusOK, gin.H{
        "code": code,
        "data": data,
        "msg": errmsg.GetErrMsg(code),
    })
}

// GetCateArt 查询分类下的所有文章
func GetCateArt(c *gin.Context) {
    pageSize, _ := strconv.Atoi(c.Query("pagesize"))
    pageNum, _ := strconv.Atoi(c.Query("pagenum"))
    id, _ := strconv.Atoi(c.Query("module"))

    switch {
    case pageSize >= 100:
        pageSize = 100
    case pageSize <= 0:
        pageSize = 10
    }

    if pageNum == 0 {
        pageNum = 1
    }

    dataArt, code, total := model.GetCateArt(id, pageSize, pageNum)

    totalPages := int(math.Ceil(float64(total) / float64(pageSize)))

    data := gin.H{
        "articles":   dataArt,
        "curPage":    pageNum,
        "totalPages": totalPages,
    }
    c.JSON(http.StatusOK, gin.H{
        "code": code,
        "data": data,
        "msg":  errmsg.GetErrMsg(code),
    })
}

// GetArtInfo 查询单个文章信息
func GetArtInfo(c *gin.Context) {
    // id, _ := strconv.Atoi(c.Param("aid"))
    id, _ := strconv.Atoi(c.Query("aid"))
    data, code := model.GetArtInfo(id)
    c.JSON(http.StatusOK, gin.H{
        "code": code,
        "data": data,
        "msg":  errmsg.GetErrMsg(code),
    })
}

// GetArt 查询文章列表
// func GetArt(c *gin.Context) {
//     pageSize, _ := strconv.Atoi(c.Query("pagesize"))
//     pageNum, _ := strconv.Atoi(c.Query("pagenum"))
//     title := c.Query("title")

//     switch {
//     case pageSize >= 100:
//         pageSize = 100
//     case pageSize <= 0:
//         pageSize = 10
//     }

//     if pageNum == 0 {
//         pageNum = 1
//     }
//     if len(title) == 0 {
//         data, code, total := model.GetArt(pageSize, pageNum)
//         c.JSON(http.StatusOK, gin.H{
//             "status":  code,
//             "data":    data,
//             "total":   total,
//             "message": errmsg.GetErrMsg(code),
//         })
//         return
//     }

//     data, code, total := model.SearchArticle(title, pageSize, pageNum)
//     c.JSON(http.StatusOK, gin.H{
//         "status":  code,
//         "data":    data,
//         "total":   total,
//         "message": errmsg.GetErrMsg(code),
//     })
// }

// EditArt 编辑文章
func EditArt(c *gin.Context) {
    var data model.Article
    id, _ := strconv.Atoi(c.Param("id"))
    _ = c.ShouldBindJSON(&data)

    code := model.EditArt(id, &data)

    c.JSON(http.StatusOK, gin.H{
        "status":  code,
        "message": errmsg.GetErrMsg(code),
    })
}

// DeleteArt 删除文章
func DeleteArt(c *gin.Context) {
    id, _ := strconv.Atoi(c.Param("aid"))

    code := model.DeleteArt(id)

    c.JSON(http.StatusOK, gin.H{
        "code":  code,
        "msg": errmsg.GetErrMsg(code),
    })
}

// SearchArticle 搜索文章
func SearchArticle(c *gin.Context) {
    keyword := c.Query("content")
    pageSize := c.DefaultQuery("pageSize", "10000000")
    pageNum := c.DefaultQuery("pageNum", "1")

    pageSizeInt, _ := strconv.Atoi(pageSize)
    pageNumInt, _ := strconv.Atoi(pageNum)

    articleList, code, total := model.SearchArticleByTitleAndContent(keyword, pageSizeInt, pageNumInt)
    if code != errmsg.SUCCESS {
        c.JSON(http.StatusInternalServerError, gin.H{
            "code": http.StatusInternalServerError,
            "msg":  errmsg.GetErrMsg(code),
        })
        return
    }

    c.JSON(http.StatusOK, gin.H{
        "code": http.StatusOK,
        "msg":  "搜索成功",
        "Data": gin.H{
            "articles": articleList,
            "num":      total,
        },
    })
}
